package p270jp.pxv.android.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.databinding.DataBindingUtil;
import java.util.Objects;
import p014ae.C0135p4;
import p014ae.C0147r4;
import p014ae.C0185y;
import p018ai.C0218e;
import p090d0.C2518n;
import p134ei.AnalyticsAction;
import p134ei.PixivAnalytics;
import p165fo.C3114c;
import p165fo.Subscribe;
import p187gl.C3361j;
import p210hh.PixivAccountManager;
import p258jd.SingleFlatMap;
import p270jp.pxv.android.R;
import p270jp.pxv.android.event.ShowFollowDialogEvent;
import p270jp.pxv.android.event.UpdateFollowEvent;
import p270jp.pxv.android.legacy.model.PixivUser;
import p274k2.ContextCompat;
import p314lg.ButtonFollowBinding;
import p370ni.Restrict;
import p569vd.C6830b;
import p593wc.Single;
import p603wm.AbstractC6949c;
import p645yc.AndroidSchedulers;
import p665z6.C7463r;
import p671zc.CompositeDisposable;

/* renamed from: jp.pxv.android.view.FollowButton */
/* loaded from: classes2.dex */
public class FollowButton extends LinearLayout implements View.OnClickListener, View.OnLongClickListener {

    /* renamed from: k */
    public static final /* synthetic */ int f19511k = 0;

    /* renamed from: b */
    public PixivUser f19513b;

    /* renamed from: c */
    public AnalyticsAction f19514c;

    /* renamed from: d */
    public AnalyticsAction f19515d;

    /* renamed from: g */
    public final int f19518g;

    /* renamed from: h */
    public final int f19519h;

    /* renamed from: i */
    public final int f19520i;

    /* renamed from: j */
    public final int f19521j;

    /* renamed from: a */
    public CompositeDisposable f19512a = new CompositeDisposable();

    /* renamed from: e */
    public ButtonFollowBinding f19516e = (ButtonFollowBinding) DataBindingUtil.m11370c(LayoutInflater.from(getContext()), R.layout.button_follow, this, true);

    /* renamed from: f */
    public AbstractC6949c<PixivAnalytics> f19517f = C2518n.m7638h(PixivAnalytics.class);

    public FollowButton(Context context, AttributeSet attributeSet) {
        super(context, attributeSet, 0, 2131887289);
        TypedArray obtainStyledAttributes = context.getTheme().obtainStyledAttributes(attributeSet, C0218e.f744b, 0, 2131887289);
        this.f19518g = obtainStyledAttributes.getColor(0, 0);
        this.f19519h = obtainStyledAttributes.getColor(2, 0);
        this.f19520i = obtainStyledAttributes.getResourceId(3, 0);
        this.f19521j = obtainStyledAttributes.getResourceId(1, 0);
        obtainStyledAttributes.recycle();
        setLongClickable(true);
        setOnLongClickListener(this);
        setOnClickListener(this);
    }

    /* renamed from: a */
    public void m4495a(PixivUser pixivUser, AnalyticsAction aVar, AnalyticsAction aVar2) {
        this.f19513b = pixivUser;
        this.f19514c = aVar;
        this.f19515d = aVar2;
        m4494b();
    }

    /* renamed from: b */
    public final void m4494b() {
        int i;
        Context context;
        if (this.f19513b.f19078id == PixivAccountManager.m5511e().f16499e) {
            setVisibility(4);
            return;
        }
        setVisibility(0);
        Context context2 = getContext();
        int i2 = this.f19513b.isFollowed ? this.f19521j : this.f19520i;
        Object obj = ContextCompat.f19963a;
        this.f19516e.f21915q.setBackground(ContextCompat.C4472c.m4259b(context2, i2));
        this.f19516e.f21915q.setTextColor(this.f19513b.isFollowed ? this.f19519h : this.f19518g);
        TextView textView = this.f19516e.f21915q;
        if (this.f19513b.isFollowed) {
            context = getContext();
            i = R.string.user_following;
        } else {
            context = getContext();
            i = R.string.user_follow;
        }
        textView.setText(context.getString(i));
    }

    @Override // android.view.View, android.view.ViewGroup
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        C3114c.m6284b().m6276j(this);
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View view) {
        if (this.f19513b != null) {
            setEnabled(false);
            PixivUser pixivUser = this.f19513b;
            if (pixivUser.isFollowed) {
                CompositeDisposable aVar = this.f19512a;
                long j = pixivUser.f19078id;
                Single<String> c = PixivAccountManager.m5511e().m5513c();
                C3361j jVar = new C3361j(j, 3);
                Objects.requireNonNull(c);
                aVar.mo82c(new SingleFlatMap(c, jVar).m966j(AndroidSchedulers.m334a()).m963m(new C0185y(this, 22), new C0135p4(this, 21)));
                return;
            }
            CompositeDisposable aVar2 = this.f19512a;
            long j2 = pixivUser.f19078id;
            Restrict dVar = Restrict.PUBLIC;
            Single<String> c2 = PixivAccountManager.m5511e().m5513c();
            C7463r rVar = new C7463r(j2, dVar);
            Objects.requireNonNull(c2);
            aVar2.mo82c(new SingleFlatMap(c2, rVar).m966j(AndroidSchedulers.m334a()).m963m(new C6830b(this, 20), new C0147r4(this, 21)));
        }
    }

    @Override // android.view.View, android.view.ViewGroup
    public void onDetachedFromWindow() {
        this.f19512a.m80f();
        C3114c.m6284b().m6274l(this);
        super.onDetachedFromWindow();
    }

    @Subscribe
    public void onEvent(UpdateFollowEvent updateFollowEvent) {
        PixivUser pixivUser = this.f19513b;
        if (pixivUser != null && pixivUser.f19078id == updateFollowEvent.getUserId()) {
            m4494b();
        }
    }

    @Override // android.view.View.OnLongClickListener
    public boolean onLongClick(View view) {
        if (this.f19513b == null) {
            return true;
        }
        C3114c.m6284b().m6280f(new ShowFollowDialogEvent(this.f19513b));
        return true;
    }
}
